package com.gxa.employment.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gxa.employment.common.Result;
import com.gxa.employment.entity.dto.EntStaDto;
import com.gxa.employment.entity.dto.EnterpriseDto;
import com.gxa.employment.entity.dto.PageBean;
import com.gxa.employment.entity.pojo.Enterprise;
import com.gxa.employment.entity.vo.EntStaffJobVo;
import com.gxa.employment.entity.vo.EnterpriseEmploymentVo;
import com.gxa.employment.entity.vo.PageVo;
import com.gxa.employment.service.EntStaffJobService;
import com.gxa.employment.service.EnterpriseEmploymentVoService;
import com.gxa.employment.service.EnterpriseService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

/**
 * <p>
 * 企业信息表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-03-25
 */
@RestController
@RequestMapping("/enterprise")
public class EnterpriseController {
    @Autowired
    private EnterpriseService enterpriseService;

    @Autowired
    EnterpriseEmploymentVoService enterpriseEmploymentVoService;

    @Autowired
    EntStaffJobService entStaffJobService;

    @PostMapping("/update")
    public Result update() {
        UpdateChainWrapper<Enterprise> update = enterpriseService.update();
        return Result.success("成功");
    }

    @PostMapping("/add")
    public Result add(@RequestBody Enterprise enterprise) {
        // 校验企业电话
        if (!isValidPhoneNumber(enterprise.getEntPhone())) {
            return Result.success("企业电话号码格式不正确");
        }
        // 校验联系人电话
        if (!isValidPhoneNumber(enterprise.getEntContactPhone())) {
            return Result.success("联系人电话号码格式不正确");
        }

        System.out.println(enterprise.toString());
        enterpriseService.save(enterprise);
        return Result.success("成功");
    }

    // 手机号码校验方法
    private boolean isValidPhoneNumber(String phoneNumber) {
        if (phoneNumber == null) {
            return false;
        }
        // 手机号码正则表达式，假设是中国大陆手机号码格式
        String regex = "^1[3-9]\\d{9}$";
        return Pattern.matches(regex, phoneNumber);
    }

    @DeleteMapping  ("/delete/{id}")
    public Result<Boolean> delete(@PathVariable Integer id) {
        QueryWrapper<Enterprise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ent_id", id);
        boolean remove = enterpriseService.remove(queryWrapper);
        return Result.success(remove);
    }



    @GetMapping("/search")
    public Result search(@RequestParam(required = false) String keyword) {
        List<Enterprise> enterprises;
        if (keyword == null || keyword.isEmpty()) {
            // 如果没有传入查询关键字，查询所有企业
            enterprises = enterpriseService.list();
        } else {
            // 如果传入了查询关键字，进行模糊查询
            enterprises = enterpriseService.listByKeyword(keyword);
        }
        return Result.success(enterprises);
    }

    @GetMapping("/page")
    public Page<Enterprise> getEnterprisePage(PageBean pageBean) {
        return enterpriseService.getEnterprisePage(pageBean);
    }

    @PostMapping(value = "/selectEntEmp",produces = "application/json")
    @Operation(summary = "查询企业员工就业信息表")
    public Result selectEntEmp(@RequestBody EnterpriseDto enterpriseDto){
        PageVo<EnterpriseEmploymentVo> enterpriseEmploymentVoPageVo = enterpriseEmploymentVoService.selectEntEmp(enterpriseDto);
        return Result.success(enterpriseEmploymentVoPageVo);
    }

    @PostMapping(value = "/selectEntSta",produces = "application/json")
    @Operation(summary = "查询企业员工岗位信息表")
    public Result selectEntSta(@RequestBody EntStaDto entStaDto){
        PageVo<EntStaffJobVo> entStaffJobVoPageVo = entStaffJobService.selectEntSta(entStaDto);
        return Result.success(entStaffJobVoPageVo);
    }
}







